﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Isha.Services;
using System.Web.Mvc;
using Isha.Entities;
using Isha.Web.Mvc;
using Isha.Infrastructure;
using Microsoft.Practices.Unity;
using System.Xml;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml.Serialization;
using Isha.Extensions;

namespace Isha.Web.Mvc
{
    public partial class IshaContext
    {
        public static IshaContext Current
        {
            get
            {
                return DependencyResolver.Current.GetService<IshaContext>();
            }
        }

        public IUnityContainer Container
        {
            get
            {
                return DependencyResolver.Current.GetService<IUnityContainer>();
            }
        }

        public IContentCommand ContentCommand
        {
            get
            {
                if (Container.IsRegistered<IContentCommand>())
                    return DependencyResolver.Current.GetService<IContentCommand>();
                else
                    return null;
            }
        }

        public ContentItem ContentItem
        {
            get
            {
                return DependencyResolver.Current.GetService<ContentItem>();
            }
            set
            {
                Container.RegisterInstance<ContentItem>(value);
            }
        }

        public Commands Command
        {
            get
            {
                return Container.Resolve<Commands>("IshaAction");
            }
        }

        public IshaServiceContext ServiceContext
        {
            get
            {
                return DependencyResolver.Current.GetService<IshaServiceContext>();
            }
        }

        public IEnumerable<ContentItem> ListContents
        {
            get
            {
                if (Container.IsRegistered<IEnumerable<ContentItem>>("List"))
                    return Container.Resolve<IEnumerable<ContentItem>>("List");
                else
                    return null;
            }
        }

        public List<ContentItem> BreadcrumbItems
        {
            get
            {
                return Container.Resolve<List<ContentItem>>("Breadcrumbs");
            }
        }

        public IshaModel Model
        {
            get
            {
                return DependencyResolver.Current.GetService<IshaModel>();
            }
        }

        public IshaConfig Configuration
        {
            get
            {
                return DependencyResolver.Current.GetService<IshaConfig>();
            }
        }

        public IEnumerable<Part> Parts
        {
            get
            {
                return DependencyResolver.Current.GetService<IEnumerable<Part>>();
            }
        }

        public IEnumerable<ContentItem> Navigation
        {
            get
            {
                if (Container.IsRegistered<IEnumerable<ContentItem>>("Navigation"))
                    return Container.Resolve<IEnumerable<ContentItem>>("Navigation");
                else
                    return null;
            }
            internal set
            {
                Container.RegisterInstance<IEnumerable<ContentItem>>("Navigation", value);
            }
        }

        public string Serializer(Type type, object obj)
        {
            if (obj == null)
                throw new InvalidOperationException("Object can't be null.");

            XmlSerializer ser = new XmlSerializer(type);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            ser.Serialize(writer, obj);
            writer.Close();
            return sb.ToString();
        }

        public object Deserializer(Type type, string xml)
        {
            if (string.IsNullOrEmpty(xml))
                return Activator.CreateInstance(type, false);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer ser = new XmlSerializer(type);
            return ser.Deserialize(reader);
        }

        public string UrlFix(string text)
        {
            //TODO: To work on this
            return Regex.Replace(text, @"\s|$|&|\.|:|<|>|[|]|\+|#|%|\@|\""|/|;|=|\?|\\|\^|~|'|,", "-",
                RegexOptions.Compiled | RegexOptions.IgnoreCase).ToLower().TrimEnd('-');
        }

        public string UrlDecode(string url)
        {
            return HttpContext.Current.Server.UrlDecode(url);
        }

        public string UrlEncode(string url)
        {
            return HttpContext.Current.Server.UrlEncode(url);
        }

        public string HtmlEncode(string html)
        {
            return HttpContext.Current.Server.HtmlEncode(html);
        }

        public string HtmlDecode(string html)
        {
            return HttpContext.Current.Server.HtmlDecode(html);
        }

        public IshaRequestContext Request
        {
            get
            {
                return new IshaRequestContext(HttpContext.Current);
            }
        }

        public IshaResponseContext Response
        {
            get
            {
                return new IshaResponseContext(HttpContext.Current);
            }
        }
    }
}
